package com.lichuandi.springbootdemo1.controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.lichuandi.springbootdemo1.service.QRCodeService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author Lichuandi
 * @version 1.0
 * @program : springbootdemo1
 * @ClassName QRCodeController
 * @description :
 * @create: 2025-01-09 15:30
 **/
@RestController("QRCodeController")
@RequestMapping("/qrcode")
@CrossOrigin
public class QRCodeController {

    @Autowired
    private QRCodeService qrCodeService;
    @GetMapping("/generateQRCode1")
    public String generateQRCode1(@RequestParam String text, @RequestParam String size) {
        // 这里是实现生成二维码的逻辑，例如使用ZXing库等。
        qrCodeService.generateQRCodeImage(text, Integer.parseInt(size), "myqrcode.png");
        return "生成的二维码图片URL";
    }

    @GetMapping("/generateQRCode")
    public ResponseEntity<Resource> generateQRCode(@RequestParam String text,@RequestParam Integer width,  @RequestParam Integer height) throws WriterException, IOException {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height);
        BufferedImage qrCodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
       ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
       ImageIO.write(qrCodeImage, "PNG", byteArrayOutputStream);
       byte[] qrCodeBytes = byteArrayOutputStream.toByteArray();
        HttpHeaders headers=  new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE);
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(qrCodeBytes.length)
                .body(new ByteArrayResource(qrCodeBytes));
    }

    @GetMapping(value = "/{barcode}",produces = MediaType.IMAGE_PNG_VALUE)
    public ResponseEntity<BufferedImage> barbecueEAN13Barcode(@PathVariable("barcode") String barcode) throws WriterException, IOException {
        QRCodeWriter barcodeWriter =  new QRCodeWriter();
        BitMatrix bitMatrix = barcodeWriter.encode(barcode, BarcodeFormat.QR_CODE, 200, 200);
        return new ResponseEntity<>(MatrixToImageWriter.toBufferedImage(bitMatrix), HttpStatus.OK);

    }

    @GetMapping(value = "/code/{url}",produces = MediaType.IMAGE_PNG_VALUE)
    public void makeQRCode(@PathVariable("url") String url, HttpServletResponse response) throws WriterException, IOException {
        qrCodeService.makeQRCode(url, response);
    }




}
